package output;

import java.util.ArrayList;
import java.util.List;
import ontology.FrameRelation;
import ontology.FrameRelationType;
import ontology.Ontology;
import ontology.Frame;
import ontology.FrameElement;
import ontology.FERelation;
import ontology.CoreSet;

/** 
 * This class implements the 'semanticrole' elements of harvested 
 * lexical entry files. These have the following recursive format:
 * 
 *   semanticrole @name @semanticType @coreType @coreset
 *     > semanticrole*
 *     
 */

public class SemanticRole {

	public String name;
	public String semanticType;
	public String coreType = "not found";
	public String coreSet = "";
	public List<SemanticRole> superRoles = new ArrayList<SemanticRole>();
	
	public SemanticRole(String s,String semtype,Ontology o) {
		name = s;
		semanticType = semtype;
		// coretype:
		for (Frame f: o.frames) {
			if (f.name.equals(semtype)) {
				for (FrameElement fe: f.frameElements) {
					if (fe.name.equals(s)) {
						coreType = fe.coreType; // defaults to "not found"
						break;
					}
				}
				break;
			}
		}
		// coreset:
		for (CoreSet c: o.coresets) {
			if (c.frame.equals(semanticType) && c.frameElements.contains(name)) {
				coreSet = c.name;
				break;
			}
		}
		// superroles:
		for (FrameRelationType r: o.relations) {
			if (r.name.equals("Inheritance")) {
				for (FrameRelation l: r.links) {
					if (l.subFrameName.equals(semtype)) {
						for (FERelation ll: l.feLinks) {
							if (ll.subFEName.equals(s)) {
								superRoles.add(new SemanticRole(ll.superFEName,l.superFrameName,o));
							}
						}
					}
				}
				break;
			}
		}
	}
	
	public SemanticRole() {}
	
	public String toXML(int n) { // recursive indentation
		String s = "";
		for (int i=0;i<n;i++) s = s + " ";
		s = s + "<semanticrole name=\"" + name + "\" semanticType=\"" + semanticType + "\" coreType=\"" + coreType + "\"";
		if (!coreSet.equals("")) s = s + " coreset=\"" + coreSet + "\"";
		if (superRoles.isEmpty()) return s + "/>\n";
		else {
			s = s + ">\n";
			for (SemanticRole r: superRoles) {
				s = s + r.toXML(n+2);
			}
			for (int i=0;i<n;i++) s = s + " ";
			return s + "</semanticrole>\n";
		}
	}
	
	public SemanticRole clone() {
		SemanticRole s = new SemanticRole();
		s.name = this.name;
		s.semanticType = this.semanticType;
		s.coreType = this.coreType;
		s.coreSet = this.coreSet;
		for (SemanticRole t: this.superRoles) {
			s.superRoles.add(t.clone());
		}
		return s;
	}
	
	public SemanticRole clone2() {
		if (this.superRoles.isEmpty()) {
			return this.clone();
		}
		else return this.superRoles.get(0).clone2();
	}
	
	public SemanticRole clone3() {
		if (this.coreSet.equals("")) {
			SemanticRole s = new SemanticRole();
			s.name = this.name;
			s.semanticType = this.semanticType;
			s.coreType = this.coreType;
			//s.coreSet = this.coreSet;
			return s;
		}
		else {
			SemanticRole s = new SemanticRole();
			s.name = this.coreSet;
			s.semanticType = this.semanticType;
			s.coreType = this.coreType;
			return s;
		}
	}
	
	/*
	public String getMostGeneralRole() {
		if (this.coreSet.equals("")) {
			SemanticRole s = new SemanticRole();
			s.name = this.name;
			return this.name;
		}
		else {
			return this.superRoles.get(0).getMostGeneralRole();
		}
	}
*/
	
}
